package com.liuzu.esys.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.liuzu.esys.bo.PrerogativesBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.common.R;
import com.liuzu.esys.pojo.Member;
import com.liuzu.esys.pojo.Prerogative;
import com.liuzu.esys.service.MemberService;
import com.liuzu.esys.service.PrerogativeService;
import com.liuzu.esys.vo.YyhPieVo;
import com.liuzu.esys.vo.YyhPieVo;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 会员的 Controller 层
 *
 */
@RestController
@RequestMapping("/Member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private PrerogativeService prerogativeService;

    /**
     * 分页查询会员功能
     */
    @RequestMapping("/MemberList")
    public LayuiPageVo<Member> MemberList(@RequestParam Map<String,Object> params){
        // 测试是否获取到参数时使用
        System.out.println(params);
        return memberService.pagingquery(params);
    }
    /**
     * 根据ID 删除功能
     */
    @RequestMapping("/MemberDel")
    public R MemberDel(Integer id){
        //测试是否可以接收到ID
//      System.out.println(id);
        boolean flag = false;
        try {
        flag = memberService.memberdel(id);
        }catch (RuntimeSqlException e){
           return R.error();
        }catch (Exception e){
            return R.error();
        }
        return R.ok();
    }
    /**
     * 添加会员功能  测试分支 测试
     */
    @GetMapping("/MemberAdd")
    public R MemberAdd(Member member){
        //测试是否可以接收到对象
        System.out.println(member);
        boolean flag = false;
        try {
        flag = memberService.memberAdd(member);
        }catch (RuntimeSqlException e){
            R.error();
        }catch (Exception e){
            R.error();
        }
            return R.ok();

    }

    /**
     * 修改会员操作
     */
    @GetMapping("/MemberSet")
    public R MemberSet(Member member){
        //测试是否可以接收到参数
        System.out.println(member);
        boolean flag = false;
        try {
        flag =  memberService.memberSet(member);
        }catch (RuntimeSqlException e){
            R.error();
        }catch (Exception e){
            R.error();
        }
           return  R.ok();
    }

    /**
     *   展示出圆柱形
     */
    @RequestMapping("/MemberBar")
    public R MemberBar(){
        List<YyhPieVo>  list  =  memberService.memberBar();
        return R.ok().data("listBar",list);
    }

    /**
     * 导出 所有会员信息
     */
    @RequestMapping("/MemberExport")
    public void export(Prerogative prerogative, HttpServletResponse response) throws IOException {
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("所有会员信息", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, PrerogativesBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备满足条件的集合数据
        List<PrerogativesBo> prerogativebos = prerogativeService.prerogativeExcelData(prerogative);
        // 把这个集合数据 写到 输出流
        sheet.doWrite(prerogativebos);
    }

}



